﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Ink;
using System.IO;

namespace DrawingCore
{
    class ToolHandDraw : ToolObject
    {
        private InkCanvas _InkCanvas;

        private DrawingCanvas _DrawCanvas;

        private SolidColorBrush Alpha;

        public ToolHandDraw(DrawingCanvas canvas){
            _InkCanvas = new InkCanvas();
            _InkCanvas.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight;
            _InkCanvas.Width = SystemParameters.FullPrimaryScreenWidth;
            Alpha = new SolidColorBrush();
            Color color = new Color();
            color.A = 0;
            Alpha.Color = color;
            _InkCanvas.MouseUp += new System.Windows.Input.MouseButtonEventHandler(_InkCanvas_MouseUp);
        }

        void _InkCanvas_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            AddNewObject(this._DrawCanvas,
                new GraphicHanddraw(_InkCanvas.Strokes.Last(), _InkCanvas));
        }

        public ToolHandDraw(){
            _InkCanvas = new InkCanvas();
        }

        public override void OnMouseDown(DrawingCanvas drawingCanvas, System.Windows.Input.MouseButtonEventArgs e)
        {
            //throw new NotImplementedException();
            SetEnable(drawingCanvas);
        }

        public override void OnMouseMove(DrawingCanvas drawingCanvas, System.Windows.Input.MouseEventArgs e)
        {
            //throw new NotImplementedException();
        }

        public override void SetCursor(DrawingCanvas drawingCanvas)
        {
            //throw new NotImplementedException();
            SetEnable(drawingCanvas);
        }

        public override void OnMouseUp(DrawingCanvas drawingCanvas, System.Windows.Input.MouseButtonEventArgs e)
        {
            //throw new NotImplementedException();
            Stroke stroke = _InkCanvas.Strokes.Last();
            StrokeCollection sc = new StrokeCollection();
            sc.Add(stroke);
            MemoryStream ms = new MemoryStream();
            sc.Save(ms);
            byte[] buffer = ms.GetBuffer();
        }

        public override void SetDisabled(DrawingCanvas drawCanvas)
        {
            //throw new NotImplementedException();
            SetDisable(drawCanvas);
        }

        public override void SetEnabled(DrawingCanvas drawCanvas)
        {
            //throw new NotImplementedException();
            SetEnable(drawCanvas);
        }

        private void SetEnable(DrawingCanvas canvas){
            if(this._DrawCanvas == null){
                this._DrawCanvas = canvas;
            }

            if(canvas.Background== Alpha)
            {
                canvas.Background = _InkCanvas.Background;
                _InkCanvas.Background = Alpha;
            }
            else
            {
                Brush brush = _InkCanvas.Background;
                this._InkCanvas.Background = Alpha;
            }

            this._InkCanvas.DefaultDrawingAttributes.Width = canvas.LineWidth;
            Panel.SetZIndex(_InkCanvas, 10);
            Panel.SetZIndex(canvas, 1);
            var parent = canvas.Parent;
            canvas.IsEnabled = false;

            if(((Grid)parent).Children.IndexOf(_InkCanvas) == -1)
                ((Grid)parent).Children.Add(_InkCanvas);
            _InkCanvas.IsEnabled = true;
            _InkCanvas.Visibility = Visibility.Visible;
            bool isFocus = _InkCanvas.IsFocused;
        }

        private void SetDisable(DrawingCanvas canvas){
            Brush brush = canvas.Background;
            _InkCanvas.Background = brush;
            canvas.Background = Alpha;
            Panel.SetZIndex(_InkCanvas, 1);
            Panel.SetZIndex(canvas, 10);
            _InkCanvas.IsEnabled = false;
            canvas.IsEnabled = true;
            canvas.Focus();
        }

        private void SaveFile(){
            Stroke stroke = _InkCanvas.Strokes.Last();
            StrokeCollection sc = new StrokeCollection();
            sc.Add(stroke);
            MemoryStream ms = new MemoryStream();
            sc.Save(ms);
            byte[] buffer = ms.GetBuffer();
        }
    }
}
